package com.media.entrance.service;

import com.media.entrance.mapper.MatchMapper;
import com.media.entrance.model.MatchAndResourceModel;
import com.media.entrance.model.MatchInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class OperateDatabaseService {
    @Resource
    private MatchMapper matchMapper;

    public MatchInfo findByAgentId(String agentId){
        return matchMapper.findByAgentId(agentId);
    }
    public MatchInfo findByInitAgentId(String agentId){
        return matchMapper.findByInitAgentId(agentId);
    }
    public MatchInfo findByAgentIdWithLock(String agentId){
        return matchMapper.findByAgentIdWithLock(agentId);
    }
    public MatchInfo findByInitAgentIdWithLock(String agentId){
        return matchMapper.findByInitAgentIdWithLock(agentId);
    }

    public MatchInfo findByRoomId(String roomId){
        return matchMapper.findByRoomId(roomId);
    }

    public MatchInfo findByAgentStatus(String agentStatus){
        return matchMapper.findByAgentStatus(agentStatus);
    }

    public List<MatchInfo> findAllByAgentStatus(String agentStatus){
        return matchMapper.findAllByAgentStatus(agentStatus);
    }

    public MatchInfo findByDeviceId(String deviceId){
        return matchMapper.findByDeviceId(deviceId);
    }
    public MatchInfo findByDeviceIdWithLock(String deviceId){
        return matchMapper.findByDeviceIdWithLock(deviceId);
    }

    public MatchInfo findOneByAgentStatus(String agentStatus){
        return matchMapper.findByAgentStatus(agentStatus);
    }
    public int deleteByAgentId(String agentId){
        return matchMapper.deleteByAgentId(agentId);
    }

    public int insertMatchInfo(MatchInfo matchInfo){
        matchInfo.setTimestamp(new Date());
        return matchMapper.insertMatchInfo(matchInfo);
    }

    public int updateMatchInfo(MatchInfo matchinfo){
        matchinfo.setTimestamp(new Date());
        return matchMapper.updateMatchInfo(matchinfo);
    }
    public int updateMatchInfo(MatchInfo matchinfo, String lastStatus){
        if (matchinfo.getAgentId() == null || matchinfo.getAgentId().equals("")) {
            log.error("updateMatchInfo must has agentId but input : " + matchinfo.getAgentId());
            return -1;
        }
        if (lastStatus == null || lastStatus.equals("")) {
            log.error("updateMatchInfo lastStatus must right: input status: " + lastStatus);
            return -1;
        }
        int result;
        matchinfo.setTimestamp(new Date());
        switch (lastStatus) {
            case "init":
                result = matchMapper.updateInitMatchInfo(matchinfo);
                break;
            case "idle":
                result = matchMapper.updateIdleMatchInfo(matchinfo);
                break;
            case "ring":
                result = matchMapper.updateRingMatchInfo(matchinfo);
                break;
            case "busy":
                result = matchMapper.updateBusyMatchInfo(matchinfo);
                break;
            case "pause":
                result = matchMapper.updatePauseMatchInfo(matchinfo);
                break;
            case "rest":
                result = matchMapper.updateRestMatchInfo(matchinfo);
                break;
            default:
                log.error("updateMatchInfo lastStatus must right: input status: " + lastStatus);
                return -1;
        }
        if (result == 0) {
            log.warn("updateMatchInfo update succcess 0 row, lastStatus:" + lastStatus);
        }
        return result;
    }

    public int insertMatchAndResourceModelToAgentRecorder(MatchAndResourceModel matchInfo){
        return matchMapper.insertMatchAndResourceModelToAgentRecorder(matchInfo);
    }

    public int insertMatchAndResourceModelToDeviceRecorder(MatchAndResourceModel matchInfo){
        return matchMapper.insertMatchAndResourceModelToDeviceRecorder(matchInfo);
    }
}
